package foundation.concurrency;

public abstract class Producer<T> implements Runnable {
	Product<T> prod = null;
	Thread thread = null;
	int maxNum = 0;
	public Producer(Product<T> prod,int maxNum) {
		this.prod = prod;
		this.maxNum=maxNum;
		thread = new Thread(this);
		prod.setMaxNum(maxNum);
	}

	abstract protected T createProduct();
	@Override
	public void run() {
		for (int i = 0; i < maxNum; i++) {
			
			T data= createProduct();
			prod.addData(data);
		}
	}

	synchronized public void start() {
		thread.start();
	}

	public synchronized void join(long milli) throws InterruptedException {
		thread.join(milli);
	}
}
